﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW.BLL
{
    public class ProviderService : RepositoryBase
    {
        public List<Provider> GetProviderByKeyword(string keyword)
        {
            List<Provider> providers=new List<Provider>();
            if (!string.IsNullOrEmpty(keyword))
            {
                providers = GetEntities<Provider>(p => p.Address.Contains(keyword) || p.City.Contains(keyword) || p.Contact1Email.Contains(keyword) || p.Contact1Fax.Contains(keyword) || p.Contact1MobilePhone.Contains(keyword)
                || p.Contact1Name.Contains(keyword) || p.Contact1Phone.Contains(keyword) || p.Contact2Email.Contains(keyword) || p.Contact2Fax.Contains(keyword) || p.Contact2MobilePhone.Contains(keyword) || p.Contact2Name.Contains(keyword)
                || p.Contact2Phone.Contains(keyword) || p.ProviderCode.Contains(keyword) || p.ProviderName.Contains(keyword) || p.ProductClass.Name.Contains(keyword) || p.Postcode.Contains(keyword)).ToList();
            }
            else
            {
                providers = GetEntities<Provider>().ToList();
            }

            return providers;
        }

        public Provider GetProviderByID(int providerID)
        {
            return GetEntities<Provider>(p => p.ProviderID == providerID).FirstOrDefault();
        }

        public List<Provider> GetAllProvider()
        {
            return GetEntities<Provider>().ToList();
        }

        public List<Provider> GetAllProviderWithSelectOne()
        {
            List<Provider> lists = new List<Provider>();
            lists.Add(new Provider { ProviderName="请选择厂商", ProviderID=0 });
            lists.AddRange(GetEntities<Provider>().ToList());
            return lists;
        }

        public List<Provider> GetAllSimpleProvider()
        {
            List<Provider> providerList = new List<Provider>();
            IQueryable<Provider> providerQueryable = GetEntities<Provider>();
            foreach (Provider provider in providerQueryable)
            {
                if (!provider.ProductClassReference.IsLoaded)
                {
                    provider.ProductClassReference.Load();
                }
                providerList.Add(provider);
            }
            return providerList;
        }

        public Provider GetProviderByCode(string code)
        {
            return GetEntities<Provider>(p => p.ProviderCode == code).FirstOrDefault();
        }

        public void AddProvider(string code, string name, int productClassID, string relation, string contact1Name,
            string contact1Phone, string contact1MobilePhone, string contact1Fax, string contact1Email,
            string contact2Name, string contact2Phone, string contact2MobilePhone, string contact2Fax,
            string contact2Email, string postCode, string city, string address, User currentOperator)
        {
            Provider provderEntity = new Provider();
            provderEntity.Address = address;
            provderEntity.City = city;
            provderEntity.Contact1Email = contact1Email;
            provderEntity.Contact1Fax = contact1Fax;
            provderEntity.Contact1MobilePhone = contact1MobilePhone;
            provderEntity.Contact1Name = contact1Name;
            provderEntity.Contact1Phone = contact1Phone;
            provderEntity.Contact2Email = contact2Email;
            provderEntity.Contact2Fax = contact2Fax;
            provderEntity.Contact2MobilePhone = contact2MobilePhone;
            provderEntity.Contact2Name = contact2Name;
            provderEntity.Contact2Phone = contact2Phone;
            provderEntity.LastUpdate = DateTime.Now;
            provderEntity.LastUpdateBy = currentOperator.UserID;
            provderEntity.Postcode = postCode;
            provderEntity.ProductClass = GetEntities<ProductClass>().Where(p => p.ID == productClassID).FirstOrDefault();
            provderEntity.ProviderCode = code;
            provderEntity.ProviderName = name;
            provderEntity.Relation = relation;
            InsertEntity(provderEntity);
        }

        public void UpdateProvide(int providerID, string code, string name, int productClassID, string relation, string contact1Name,
            string contact1Phone, string contact1MobilePhone, string contact1Fax, string contact1Email,
            string contact2Name, string contact2Phone, string contact2MobilePhone, string contact2Fax,
            string contact2Email, string postCode, string city, string address, User currentOperator)
        {
            Provider provderEntity = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            provderEntity.Address = address;
            provderEntity.City = city;
            provderEntity.Contact1Email = contact1Email;
            provderEntity.Contact1Fax = contact1Fax;
            provderEntity.Contact1MobilePhone = contact1MobilePhone;
            provderEntity.Contact1Name = contact1Name;
            provderEntity.Contact1Phone = contact1Phone;
            provderEntity.Contact2Email = contact2Email;
            provderEntity.Contact2Fax = contact2Fax;
            provderEntity.Contact2MobilePhone = contact2MobilePhone;
            provderEntity.Contact2Name = contact2Name;
            provderEntity.Contact2Phone = contact2Phone;
            provderEntity.LastUpdate = DateTime.Now;
            provderEntity.LastUpdateBy = currentOperator.UserID;
            provderEntity.Postcode = postCode;
            provderEntity.ProductClass = GetEntities<ProductClass>().Where(p => p.ID == productClassID).FirstOrDefault();
            provderEntity.ProviderCode = code;
            provderEntity.ProviderName = name;
            provderEntity.Relation = relation;
            UpdateEntity(provderEntity);
        }

        public void AddDocument(int providerID, string fileName, string fileExtension, string fileGUID, string type, User currentOperator)
        {
            ProviderDocument providerDocumentEntity = new ProviderDocument();
            providerDocumentEntity.FileExtension = fileExtension;
            providerDocumentEntity.FileGUID = fileGUID;
            providerDocumentEntity.FileName = fileName;
            providerDocumentEntity.LastUpdate = DateTime.Now;
            providerDocumentEntity.LastUpdateBy = currentOperator.UserID;
            providerDocumentEntity.Type = type;
            providerDocumentEntity.Provider = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            InsertEntity(providerDocumentEntity);
        }

        public List<ProviderDocumentModel> GetProviderDocument(int providerID, string type)
        {
            List<ProviderDocumentModel> documentsModel = new List<ProviderDocumentModel>();
            IQueryable<ProviderDocument> documentEntities = GetEntities<ProviderDocument>().Where(p => p.Provider.ProviderID == providerID && p.Type.Equals(type));
            foreach (ProviderDocument documentEntity in documentEntities)
            {
                ProviderDocumentModel documentModel = new ProviderDocumentModel();
                documentModel.ID = documentEntity.ID.ToString();
                documentModel.Name = documentEntity.FileName;
                documentModel.LastUpdate = documentEntity.LastUpdate.ToString();
                User user = GetEntities<User>().Where(u => u.UserID == documentEntity.LastUpdateBy).FirstOrDefault();
                documentModel.Operator = user.UserName;
                documentModel.NameOnServer = documentEntity.FileGUID;
                documentsModel.Add(documentModel);
            }
            return documentsModel;
        }

        public void DeleteDocument(int documentID)
        {
            DeleteEntities<ProviderDocument>(d => d.ID == documentID, Guid.Empty);
        }

        public void AddSparePart(int providerID, string modelNO, string versionNO, int count, User currentOperator)
        {
            ProviderSparePart providerSparePartEntity = new ProviderSparePart();
            providerSparePartEntity.Count = count;
            providerSparePartEntity.LastUpdate = DateTime.Now;
            providerSparePartEntity.LastUpdateBy = currentOperator.UserID;
            providerSparePartEntity.ModelNumber = modelNO;
            providerSparePartEntity.Provider = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            providerSparePartEntity.VersionNumber = versionNO;
            InsertEntity(providerSparePartEntity);

            ProviderSparePartRecord recordEntity = new ProviderSparePartRecord();
            recordEntity.Count = count;
            recordEntity.OperationDatetime = DateTime.Now;
            recordEntity.OperationType = "提供";
            recordEntity.OperatorID = currentOperator.UserID;
            recordEntity.ProviderSparePart = GetEntities<ProviderSparePart>().Where(p => p.ID == providerSparePartEntity.ID).FirstOrDefault();
            InsertEntity(recordEntity);
        }

        public IQueryable<ProviderSparePart> GetSparePartOfProvider(int providerID)
        {
            return GetEntities<ProviderSparePart>().Where(s => s.Provider.ProviderID == providerID);
        }

        public ProviderSparePart GetSparePartByID(int sparePartID)
        {
            return GetEntities<ProviderSparePart>().Where(s => s.ID == sparePartID).FirstOrDefault();
        }

        public void BackSparePart(int sparePartID, int backCount, User currentOperator)
        {
            ProviderSparePart sparePartEntity = GetEntities<ProviderSparePart>().Where(s => s.ID == sparePartID).FirstOrDefault();
            sparePartEntity.Count -= backCount;
            sparePartEntity.LastUpdateBy = currentOperator.UserID;
            UpdateEntity(sparePartEntity);

            ProviderSparePartRecord recordEntity = new ProviderSparePartRecord();
            recordEntity.Count = backCount;
            recordEntity.OperationDatetime = DateTime.Now;
            recordEntity.OperationType = "退回";
            recordEntity.OperatorID = currentOperator.UserID;
            recordEntity.ProviderSparePart = sparePartEntity;
            InsertEntity(recordEntity);
        }

        public List<ProviderSparePartRecordModel> GetSparePartRecordModel(int sparePartID)
        {
            List<ProviderSparePartRecordModel> recordsModel = new List<ProviderSparePartRecordModel>();
            IQueryable<ProviderSparePartRecord> recordsEntity = GetEntities<ProviderSparePartRecord>().Where(s => s.ProviderSparePart.ID == sparePartID);
            foreach (ProviderSparePartRecord recordEntity in recordsEntity)
            {
                ProviderSparePartRecordModel recordModel = new ProviderSparePartRecordModel();
                recordModel.Count = recordEntity.Count.ToString();
                recordModel.ID = recordEntity.ID.ToString();
                if (!recordEntity.ProviderSparePartReference.IsLoaded)
                {
                    recordEntity.ProviderSparePartReference.Load();
                }
                recordModel.ModelNumber = recordEntity.ProviderSparePart.ModelNumber;
                recordModel.VersionNumber = recordEntity.ProviderSparePart.VersionNumber;
                recordModel.OperationDatetime = recordEntity.OperationDatetime.ToString();
                recordModel.Operator = GetEntities<User>().Where(u => u.UserID == recordEntity.OperatorID).FirstOrDefault().UserName;
                recordModel.OPerationType = recordEntity.OperationType;
                recordsModel.Add(recordModel);
            }
            return recordsModel;
        }

        public void AddAccessory(int providerID, string accessoryName, int count, decimal price, decimal customerPrice ,User currentOperator)
        {
            Provider providerEntity = GetEntities<Provider>().Where(p => p.ProviderID == providerID).FirstOrDefault();
            ProviderAccessory newProviderAccessory = new ProviderAccessory();
            newProviderAccessory.Count = count;
            newProviderAccessory.LastUpdate = DateTime.Now;
            newProviderAccessory.LastUpdateBy = currentOperator.UserID;
            newProviderAccessory.ModelNumber = accessoryName;
            newProviderAccessory.Provider = providerEntity;
            newProviderAccessory.Price = price;
            newProviderAccessory.CustomerPrice = customerPrice;
            newProviderAccessory.IsDeleted = false;
            InsertEntity(newProviderAccessory);

            ProviderAccessoryRecord accessoryAccord = new ProviderAccessoryRecord();
            accessoryAccord.Count = count;
            accessoryAccord.OperationDatetime = DateTime.Now;
            accessoryAccord.OperationType = "提供";
            accessoryAccord.OperatorID = currentOperator.UserID;
            accessoryAccord.ProviderAccessory = GetEntities<ProviderAccessory>().Where(a => a.ID == newProviderAccessory.ID).FirstOrDefault();
            InsertEntity(accessoryAccord);
        }

        public void UpdateAccessory(ProviderAccessory accessory, User currentOperator)
        {
            UpdateEntity(accessory);

            ProviderAccessoryRecord accessoryAccord = new ProviderAccessoryRecord();
            accessoryAccord.Count = 0;
            accessoryAccord.OperationDatetime = DateTime.Now;
            accessoryAccord.OperationType = "修改可用状态";
            accessoryAccord.OperatorID = currentOperator.UserID;
            accessoryAccord.ProviderAccessory = GetEntities<ProviderAccessory>().Where(a => a.ID == accessory.ID).FirstOrDefault();
            InsertEntity(accessoryAccord);
        }

        public IQueryable<ProviderAccessory> GetAccessoryOfProvider(int providerID)
        {
            return GetEntities<ProviderAccessory>().Where(p => p.Provider.ProviderID == providerID && p.IsDeleted != true);
        }

        public IQueryable<ProviderAccessory> GetAccessoryByPrefix(string prefix, int count)
        {
            return GetEntities<ProviderAccessory>().Where(p => p.ModelNumber.StartsWith(prefix) && p.IsDeleted != true).Take(count);
        }

        public ProviderAccessory GetAccessoryByName(string modelNumber)
        {
            return GetEntities<ProviderAccessory>().Where(p => p.ModelNumber.Equals(modelNumber) && p.IsDeleted != true).FirstOrDefault();
        }

        public void BackAccessory(int accessoryID, int backCount, User currentOperator)
        {
            ProviderAccessory accessoryEntity = GetEntities<ProviderAccessory>().Where(a => a.ID == accessoryID).FirstOrDefault();
            accessoryEntity.Count -= backCount;
            accessoryEntity.LastUpdate = DateTime.Now;
            accessoryEntity.LastUpdateBy = currentOperator.UserID;
            UpdateEntity(accessoryEntity);

            ProviderAccessoryRecord accessoryRecord = new ProviderAccessoryRecord();
            accessoryRecord.Count = backCount;
            accessoryRecord.OperationDatetime = DateTime.Now;
            accessoryRecord.OperationType = "退回";
            accessoryRecord.OperatorID = currentOperator.UserID;
            accessoryRecord.ProviderAccessory = GetEntities<ProviderAccessory>().Where(a => a.ID == accessoryID).FirstOrDefault();
            InsertEntity(accessoryRecord);
        }

        public ProviderAccessory GetAccessoryByID(int accessoryID)
        {
            return GetEntities<ProviderAccessory>(p => p.ID == accessoryID).FirstOrDefault();
        }

        public List<ProviderAccessoryRecordModel> GetAccessoryRecords(int accessoryID)
        {
            List<ProviderAccessoryRecordModel> accessoryRecordsModel = new List<ProviderAccessoryRecordModel>();
            IQueryable<ProviderAccessoryRecord> accessoryRecordsEntities = GetEntities<ProviderAccessoryRecord>().Where(ar => ar.ProviderAccessory.ID == accessoryID);
            foreach (ProviderAccessoryRecord recordEntity in accessoryRecordsEntities)
            {
                ProviderAccessoryRecordModel recordModel = new ProviderAccessoryRecordModel();
                recordModel.Count = recordEntity.Count.ToString();
                recordModel.ID = recordEntity.ID.ToString();
                if (!recordEntity.ProviderAccessoryReference.IsLoaded)
                {
                    recordEntity.ProviderAccessoryReference.Load();
                }
                recordModel.ModelNumber = recordEntity.ProviderAccessory.ModelNumber;
                recordModel.OperationDatetime = recordEntity.OperationDatetime.ToString();
                recordModel.OPerationType = recordEntity.OperationType;
                recordModel.Operator = GetEntities<User>().Where(u => u.UserID == recordEntity.OperatorID).FirstOrDefault().UserName;
                accessoryRecordsModel.Add(recordModel);
            }
            return accessoryRecordsModel;
        }

        public Provider GetProviderBySerialNOAndVersion(string serialNO, string versionNO)
        {
            ProviderProduct providerProduct = GetEntities<ProviderProduct>().Where(p => p.MaterialNumber == serialNO && p.VersionNumber == versionNO).FirstOrDefault();
            if (providerProduct != null)
            {
                if (!providerProduct.ProviderReference.IsLoaded)
                {
                    providerProduct.ProviderReference.Load();
                }
                return providerProduct.Provider;
            }
            else
            {
                return null;
            }
        }

        public List<Document> GetDocumentByProductTypeID(int productTypeID)
        {
            return GetEntities<Document>().Where(p => p.ProductType.ProductTypeID == productTypeID).ToList();
        }

        public List<Document> GetDocumentByProblemTypeID(int problemTypeID)
        {
            return GetEntities<Document>().Where(p => p.ProblemType.ID == problemTypeID).ToList();
        }

        public List<Provider> GetProviderByProductClassID(int productClassID)
        {
            return GetEntities<Provider>().Where(p => p.ProductClass.ID == productClassID).ToList();
        }
    }
}